package com.zhangdi.demo.jdbc.impl;

import com.zhangdi.demo.jdbc.entity.Industry;
import com.zhangdi.demo.jdbc.mapper.IndustryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ReadServiceImpl implements ReadService {
    @Autowired
    IndustryMapper mapper;

    void nodeLevels(Industry item, long id) {
        List<Industry> brothers = mapper.findBrothers(id,0);
        for (Industry brother : brothers) {
            if(brother.isLevel(item.getLevel_x())) {
                item.getIds().add(brother.getId());
            }
        }

        nodeLevels(item, item.getParent_id());
    }

    public Object read() {
        List<Industry> levelList = mapper.findAllLevelsByLevelType(2);
        for (Industry item : levelList) {
            List<Industry> brothers = mapper.findBrothers(item.getId(), item.getParent_id());
            for (Industry brother : brothers) {
                    item.getIds().add(brother.getId());
            }

            //父节点的兄弟节点
//            brothers = mapper.findBrothers(item.getParent_id());
            //兄弟节点的等级
            for (Industry brother : brothers) {
                if(brother.isLevel(item.getLevel_x())){
                    item.getIds().add(brother.getId());
                }
            }
        }

        return levelList;
    }


    public boolean isBottom(List<Industry> children) {
        return children.get(0).isLevel();
    }
}
